package com.gxgeek.springbootquartz.task;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.quartz.TriggerKey;
import org.quartz.listeners.JobListenerSupport;

import java.util.Date;

/**
 * @author gaoxing
 * @create 2017-12-12 上午10:49
 */
@Slf4j
@Data
@AllArgsConstructor
public class QuartzJobListener extends JobListenerSupport {

//    private JobManagerService jobManagerService;

    private String name;

//    public QuartzJobListener(String quartzListener, JobManagerService jobManagerService) {
//        this.name = quartzListener;
//        this.jobManagerService = jobManagerService;
//    }


    @Override
    public void jobWasExecuted(JobExecutionContext context,
                               JobExecutionException jobException) {
        try {
            JobKey jobKey = context.getJobDetail().getKey();

            TriggerKey triggerKey = context.getTrigger().getKey();

            Date fireTime = context.getFireTime();

            Class jobClass = context.getJobDetail().getJobClass();

            log.info("JobClass:{},Job:{},Trigger:{},FireTime:{}", jobClass, jobKey, triggerKey, fireTime);
            //保存执行记录
//            jobManagerService.saveScheduleHis(context);
            //保存Job异常
//            jobManagerService.saveScheduleError(context,jobException);
        } catch (Exception e) {
            log.error("QuartzListener jobWasExcuted exception: ", e);
        }
    }
}
